import roleSelect from './roleSelect';
import { getRoleList } from '@/api/system/tbSysRole';
import { addRolesByUsers } from '@/api/system/tbSysUserRole';
import { banAndRecoveryUser, updateUserPassword } from '@/api/system/tbSysUser';
export default {
	components: { roleSelect },
	data() {
		return {
			roleListData: [],
			checkedRoles: null
		}
	},
	async created() {
		await this.getRoleListData();
	},
	methods: {
		//获取角色列表数据
		async getRoleListData() {
			let params = { page: 1, limit: 9999 };
			let result = await getRoleList(params).then((res) => res.data);
			if (result.code === '0') {
				this.roleListData = result.data || [];
			}
		},

		//封禁/恢复
		handleBanAndRecoveryUser(id, del, title) {
			let that = this;
			this.$confirm(`此操作将${title}该用户, 是否继续？`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						let data = { id, del, title };
						instance.confirmButtonLoading = true;
						await that.banAndRecoveryRequest(data);
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//封禁/恢复请求
		async banAndRecoveryRequest(data) {
			let params = [{ id: data.id, del: data.del }];
			let result = await banAndRecoveryUser(params).then(res => res.data);
			if (result.code === '0') {
				this.$message.success(`${data.title}账号成功`);
				this.queryTableData();
			}
		},

		//重置用户密码
		handleResetUserPassword(id) {
			let that = this;
			this.$confirm(`此操作将重置该用户密码, 是否继续？`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await that.resetUserPasswordRequest(id);
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//重置用户密码请求
		async resetUserPasswordRequest(id) {
			let params = [{ id, password: this.dictData['USER_PASSWORD'] }];
			let result = await updateUserPassword(params).then(res => res.data);
			if (result.code === '0') {
				this.$message.success(`重置密码成功`);
				this.queryTableData();
			}
		},

		//创建角色选择组件
		createRoleSelect(arr) {
			this.checkedRoles = [...arr];
			return this.$createElement('roleSelect', {
				props: {
					multiple: true,
					list: this.roleListData,
					value: this.checkedRoles
				},
				style: { width: '100%' },
				on: {
					input: (value) => {
						this.checkedRoles = value;
					}
				}
			})
		},

		//格式化配置用户角色参数
		formatConfigUserRolesParams(id) {
			let params = [];
			let roleIds = this.checkedRoles || [];
			let userIds = !id ? this.selectedUser.map(n => n.id) : [id];
			let userRoles = roleIds.map(n => userIds.map(m => ({ userId: m, roleIds: n })));
			return userRoles.reduce((prev, cur) => prev.concat(cur), []);
		},

		//配置用户角色请求
		async configUserRolesRequest(id) {
			let params = this.formatConfigUserRolesParams(id);
			let result = await addRolesByUsers(params).then(res => res.data);
			if (result.code === '0') {
				this.$message.success(`配置角色成功`);
				this.checkedRoles = [];
				this.queryTableData();
			}
		},

		//配置用户角色
		handleConfigUserRoles(data) {
			let dataArr = data.menuRoleList.map(n => n.id);
			this.$msgbox({
				title: '消息',
				message: this.createRoleSelect(dataArr),
				showCancelButton: true,
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				beforeClose: async (action, instance, done) => {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await this.configUserRolesRequest(data.id);
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//批量配置角色
		handleBatchConfigRole() {
			let dataArr = [];
			this.$msgbox({
				title: '配置角色',
				message: this.createRoleSelect(dataArr),
				showCancelButton: true,
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				beforeClose: async (action, instance, done) => {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await this.configUserRolesRequest();
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//批量配置权限
		handleBatchConfigAuth() {},

		//批量配置部门
		handleBatchConfigDept() {},
	}
}